<template>
	<view class="pay-type-list">

		<view class="type-item b-b" v-for="item in payments" :key="item.id" @click="clickpay(item.code)" v-if="!(type == 2 && item.code == 'balancepay')">
			<text v-if=" item.code == 'wechatpay'" class="icon yticon icon-weixinzhifu"></text>
			<text v-if=" item.code == 'alipay'" class="icon yticon icon-alipay"></text>
			<text v-if=" item.code == 'offline'" class="icon yticon icon-weixinzhifu"></text>
			<text v-if=" item.code == 'union'" class="icon yticon icon-weixinzhifu"></text>
			<!-- 余额支付 -->
			<text v-if=" item.code == 'balancepay'" class="icon yticon icon-erjiye-yucunkuan"></text>
			<!-- 水晶支付 -->
			<text v-if=" item.code == 'amountpay'" class="icon yticon icon-erjiye-yucunkuan"></text>
			<view class="con">
				<text class="tit">{{ item.name }}</text>
			</view>
		</view>
		<payok :show="payShow" :money="money" @close="payShow = false" @keyPwd="keyPwd" :paySize="1"></payok>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/config/config.js'
	import mallplusCopyright from '@/components/mall-copyright/mallplusCopyright.vue';
	import Api from '@/common/api';
	import payok from './payKeyBoard.vue'
	export default {
		props: {
			// 如果是商品订单此参数必须
			orderId: {
				type: String,
				default () {
					return ''
				}
			},
			// 如果是充值订单此参数必须
			recharge: {
				type: Number,
				default () {
					return 0
				}
			},
			// 用户id
			uid: {
				type: Number,
				default () {
					return 0
				}
			},
			// 订单类型
			type: {
				type: Number,
				default () {
					return 1
				}
			},
			money: {
				type: String,
				default () {
					return '00'
				}
			},
			// 商品图片
			pic:{
				type: String,
				default () {
					return ''
				}
			}
		},
		data() {
			return {
				payments: [],
				openid: '',
				payShow: false,
				code:''
			}
		},
		components: {
			payok
		},
		mounted() {
			this.getPayments()
		},
		methods: {
			// 获取可用支付方式列表
			async getPayments() {
				let params = {};
				this.orderInfo = await Api.apiCall('get', Api.order.paymentlist, params);
				this.payments = this.formatPayments(this.orderInfo)
				//console.log('支付方式列表', this.payments)
			},
			// 支付方式处理
			formatPayments(payments) {
				// h5支付并且是在微信浏览器内 过滤支付宝支付
				if (this.$common.isWeiXinBrowser()) {
					payments = payments.filter(item => item.code !== 'alipay')
					payments = payments.filter(item => item.code !== 'wechatapppay')
					payments = payments.filter(item => item.code !== 'wechatappletpay')
				}
				payments = payments.filter(item => item.code !== 'wechatapppay')
				payments = payments.filter(item => item.code !== 'wechatappletpay')
				// 如果是充值订单 过滤余额支付 过滤非线上支付方式
				if (this.type === 2) {
					payments = payments.filter(
						item => item.code !== 'balancepay' || item.is_online === 1
					)
				}

				// 设置logo图片
				payments.forEach(item => {
					this.$set(item, 'icon', '/static/image/' + item.code + '.png')
				})
				return payments
			},
			checkWXJSBridge(data) {
				let that = this
				let interval = setInterval(() => {
					if (typeof window.WeixinJSBridge != 'undefined') {
						clearTimeout(interval)
						that.onBridgeReady(data)
					}
				}, 200)
			},
			onBridgeReady(data) {
				var _this = this
				window.WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						appId: data.appId, // 公众号名称，由商户传入
						timeStamp: data.timeStamp, // 时间戳，自1970年以来的秒数
						nonceStr: data.nonceStr, // 随机串
						package: data.package,
						signType: data.signType, // 微信签名方式：
						paySign: data.paySign // 微信签名
					},
					function(res) {
						if (res.err_msg === 'get_brand_wcpay_request:ok') {
							_this.$common.successToShow('支付成功')
						} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
							_this.$common.errorToShow('取消支付')
						} else {
							_this.$common.errorToShow('支付失败')
						}
						setTimeout(() => {
							_this.$common.redirectTo(
								'/pages/order/payment/result?id=' + data.payment_id
							)
						}, 1000)
					}
				)
			},
			keyPwd(key){
				this.toPayHandler(this.code,key)
			},
			
			clickpay(code){
				this.code = code;
				this.payShow = true;
			},
			// 用户点击支付方式处理
			async toPayHandler(code,key) {
				let params = {
					'orderId': this.orderId,
					'payPwd':key
				};
				let data = {
					payment_code: code,
					payment_type: this.type,
					'payPwd':key
				}
				data['orderId'] = (this.type == 1 || this.type == 5 || this.type == 6) ? this.orderId : this.uid
				switch (code) {
					case 'alipay':
						let res = await Api.apiCall('get', Api.order.aliWapPay, params,true);
						if (res) {
							let divDom = document.createElement('div')
							divDom.innerHTML = res;
							document.body.appendChild(divDom)

							let testForm = document.getElementsByName('punchout_form')
							testForm[0].dispatchEvent(new Event('submit'))
							testForm[0].submit()
							document.body.removeChild(testForm[0])
						}
						this.payShow = false;
						break
					case 'wechatpay':
						/**
						 * 微信支付有两种
						 * 判断是否在微信浏览器
						 * 	微信jsapi支付
						 */
						let isWeiXin = this.$common.isWeiXinBrowser()
						if (isWeiXin) {
							var transitUrl =
								baseUrl +
								'wap/#/pages/order/payment/auth?order_id=' +
								this.orderId +
								'&type=' +
								this.type;
							if (this.type == 1 && this.orderId) {
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									url: transitUrl
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									money: this.recharge,
									url: transitUrl + '&uid=' + this.uid + '&money=' + this.recharge
								}
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									orderId: this.orderId
								}
							}
							let res = await Api.apiCall('get', Api.order.webPay, params,true);
							if (res) {
								this.checkWXJSBridge(res)
							} else {
								window.location.href = res.msg
								return;
							}
							
						} else {
							// 微信 H5支付  浏览器点击微信支付 type=1
							if (this.type == 1 && this.orderId) {
								data['params'] = {
									trade_type: 'MWEB',
									return_url: baseUrl +
										'wap/#/pages/order/payment/result'
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'MWEB',
									money: this.recharge,
									return_url: baseUrl + 'wap/#/pages/order/payment/result'
								}
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									orderId: this.orderId
								}
							}
							// 微信h5支付
							let res = await Api.apiCall('get', Api.order.wapPay, params,true);
							if (res) {
								if (res) {
									location.href = res
								} else {
									this.$common.errorToShow(res)
								}
							}
						}
						this.payShow = false;
						break
					case 'balancepay':
						/*
						 *
						 *  用户余额支付
						 *
						 */
						if ((this.type == 5 || this.type == 6) && this.recharge) {
							data['params'] = {
								orderId: this.orderId
							}
						}
						let params1 = {
							'orderId': this.orderId,
							'payPwd':key
						};
						let data1 = await Api.apiCall('post', Api.order.balancePay, params1,true);
						if (data1) {
							uni.redirectTo({
								url: '/pages/order/payment/result?order=' + JSON.stringify(data1)
							})
						} else {
							this.$api.msg(data1.data);
						}
						this.payShow = false;
						break
					case 'amountpay':
						/*
						 *
						 *  用户水晶支付
						 *
						 */
						if ((this.type == 5 || this.type == 7) && this.recharge) {
							data['params'] = {
								orderId: this.orderId
							}
						}
						let params2 = {
							'orderId': this.orderId,
							'payPwd':key
						};
						let data2 = await Api.apiCall('post', Api.order.amountPay, params2,true);
						if (data2) {
							uni.redirectTo({
								url: '/pages/order/payment/result?order=' + JSON.stringify(data2)
							})
						} else {
							this.$api.msg(data2.data);
						}
						this.payShow = false;
						break
					case 'offline':
						/**
						 * 线下支付
						 */
						this.payShow = false;
						this.$common.modelShow(
							'线下支付说明',
							'请联系客服进行线下支付qq:951449465',
							() => {},
							false,
							'取消',
							'确定'
						)
						
						break
				}
			}
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price {
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;

			&:before {
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;

		.type-item {
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position: relative;
		}

		.icon {
			width: 100upx;
			font-size: 52upx;
		}

		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}

		.icon-weixinzhifu {
			color: #36cb59;
		}

		.icon-alipay {
			color: #01aaef;
		}

		.tit {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}

		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}

	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
